<?php

class Order extends AppModel {

    var $alias = 'Order';
    var $name = 'Order';
    var $useTable = 'orders';
    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'fields' => array('User.id', 'User.username'),
        ),
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function getTotalPrice($date_start, $date_end) {
        $conditions =   array();
        $conditions['Order.payment_status'] =   '2';
        if($date_start){
            $conditions['Order.checkout_time >= ?'] = $date_start;
        }
        if($date_end){
            $conditions['Order.checkout_time <= ?'] = $date_end;
        }
        $data = $this->field("SUM(Order.total_price)",
                     $conditions
        );
        if($data){
            return $data;
        }else{
            return false;
        }
    }
    function getTotalOrder($date_start, $date_end){
        $conditions =   array();
        if($date_start){
            $conditions['Order.checkout_time >= ?'] = $date_start;
        }
        if($date_end){
            $conditions['Order.checkout_time <= ?'] = $date_end;
        }
        $data = $this->field("COUNT(Order.id)", $conditions);
        if($data){
            return $data;
        }else{
            return false;
        }
    }
    function getCompleteOrder($date_start, $date_end){
        $conditions =   array();
        $conditions['Order.payment_status'] =   '2';
        if($date_start){
            $conditions['Order.checkout_time >= ?'] = $date_start;
        }
        if($date_end){
            $conditions['Order.checkout_time <= ?'] = $date_end;
        }
        $data = $this->field("COUNT(Order.id)", $conditions);
        if($data){
            return $data;
        }else{
            return false;
        }
    }
}

?>
